home *** CD-ROM | disk | FTP | other *** search
/ American Osteopathic Ass…tion Yearbook 2005 & 2006 / American Osteopathic Association Yearbook 2005 & 2006.iso / mac / app / ntpath.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2004-07-22  |  14.7 KB  |  474 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.3)
  3.  
  4. '''Common pathname manipulations, WindowsNT/95 version.
  5.  
  6. Instead of importing this module directly, import os and refer to this
  7. module as os.path.
  8. '''
  9. import os
  10. import stat
  11. import sys
  12. __all__ = [
  13.     'normcase',
  14.     'isabs',
  15.     'join',
  16.     'splitdrive',
  17.     'split',
  18.     'splitext',
  19.     'basename',
  20.     'dirname',
  21.     'commonprefix',
  22.     'getsize',
  23.     'getmtime',
  24.     'getatime',
  25.     'getctime',
  26.     'islink',
  27.     'exists',
  28.     'isdir',
  29.     'isfile',
  30.     'ismount',
  31.     'walk',
  32.     'expanduser',
  33.     'expandvars',
  34.     'normpath',
  35.     'abspath',
  36.     'splitunc',
  37.     'curdir',
  38.     'pardir',
  39.     'sep',
  40.     'pathsep',
  41.     'defpath',
  42.     'altsep',
  43.     'extsep',
  44.     'realpath',
  45.     'supports_unicode_filenames']
  46. curdir = '.'
  47. pardir = '..'
  48. extsep = '.'
  49. sep = '\\'
  50. pathsep = ';'
  51. altsep = '/'
  52. defpath = '.;C:\\bin'
  53. if 'ce' in sys.builtin_module_names:
  54.     defpath = '\\Windows'
  55. elif 'os2' in sys.builtin_module_names:
  56.     altsep = '/'
  57.  
  58.  
  59. def normcase(s):
  60.     '''Normalize case of pathname.
  61.  
  62.     Makes all characters lowercase and all slashes into backslashes.'''
  63.     return s.replace('/', '\\').lower()
  64.  
  65.  
  66. def isabs(s):
  67.     '''Test whether a path is absolute'''
  68.     s = splitdrive(s)[1]
  69.     if s != '':
  70.         pass
  71.     return s[:1] in '/\\'
  72.  
  73.  
  74. def join(a, *p):
  75.     '''Join two or more pathname components, inserting "\\" as needed'''
  76.     path = a
  77.     for b in p:
  78.         b_wins = 0
  79.         if path == '':
  80.             b_wins = 1
  81.         elif isabs(b):
  82.             if path[1:2] != ':' or b[1:2] == ':':
  83.                 b_wins = 1
  84.             elif len(path) > 3 and len(path) == 3 and path[-1] not in '/\\':
  85.                 b_wins = 1
  86.             
  87.         
  88.         if b_wins:
  89.             path = b
  90.             continue
  91.         if not len(path) > 0:
  92.             raise AssertionError
  93.         if path[-1] in '/\\':
  94.             if b and b[0] in '/\\':
  95.                 path += b[1:]
  96.             else:
  97.                 path += b
  98.         b[0] in '/\\'
  99.         if path[-1] == ':':
  100.             path += b
  101.             continue
  102.         if b:
  103.             if b[0] in '/\\':
  104.                 path += b
  105.             else:
  106.                 path += '\\' + b
  107.         b[0] in '/\\'
  108.         path += '\\'
  109.     
  110.     return path
  111.  
  112.  
  113. def splitdrive(p):
  114.     '''Split a pathname into drive and path specifiers. Returns a 2-tuple
  115. "(drive,path)";  either part may be empty'''
  116.     if p[1:2] == ':':
  117.         return (p[0:2], p[2:])
  118.     
  119.     return ('', p)
  120.  
  121.  
  122. def splitunc(p):
  123.     """Split a pathname into UNC mount point and relative path specifiers.
  124.  
  125.     Return a 2-tuple (unc, rest); either part may be empty.
  126.     If unc is not empty, it has the form '//host/mount' (or similar
  127.     using backslashes).  unc+rest is always the input path.
  128.     Paths containing drive letters never have an UNC part.
  129.     """
  130.     if p[1:2] == ':':
  131.         return ('', p)
  132.     
  133.     firstTwo = p[0:2]
  134.     if firstTwo == '//' or firstTwo == '\\\\':
  135.         normp = normcase(p)
  136.         index = normp.find('\\', 2)
  137.         if index == -1:
  138.             return ('', p)
  139.         
  140.         index = normp.find('\\', index + 1)
  141.         if index == -1:
  142.             index = len(p)
  143.         
  144.         return (p[:index], p[index:])
  145.     
  146.     return ('', p)
  147.  
  148.  
  149. def split(p):
  150.     '''Split a pathname.
  151.  
  152.     Return tuple (head, tail) where tail is everything after the final slash.
  153.     Either part may be empty.'''
  154.     (d, p) = splitdrive(p)
  155.     i = len(p)
  156.     while i and p[i - 1] not in '/\\':
  157.         i = i - 1
  158.     (head, tail) = (p[:i], p[i:])
  159.     head2 = head
  160.     while head2 and head2[-1] in '/\\':
  161.         head2 = head2[:-1]
  162.     if not head2:
  163.         pass
  164.     head = head
  165.     return (d + head, tail)
  166.  
  167.  
  168. def splitext(p):
  169.     '''Split the extension from a pathname.
  170.  
  171.     Extension is everything from the last dot to the end.
  172.     Return (root, ext), either part may be empty.'''
  173.     i = p.rfind('.')
  174.     if i <= max(p.rfind('/'), p.rfind('\\')):
  175.         return (p, '')
  176.     else:
  177.         return (p[:i], p[i:])
  178.  
  179.  
  180. def basename(p):
  181.     '''Returns the final component of a pathname'''
  182.     return split(p)[1]
  183.  
  184.  
  185. def dirname(p):
  186.     '''Returns the directory component of a pathname'''
  187.     return split(p)[0]
  188.  
  189.  
  190. def commonprefix(m):
  191.     '''Given a list of pathnames, returns the longest common leading component'''
  192.     if not m:
  193.         return ''
  194.     
  195.     prefix = m[0]
  196.     for item in m:
  197.         for i in range(len(prefix)):
  198.             if prefix[:i + 1] != item[:i + 1]:
  199.                 prefix = prefix[:i]
  200.                 if i == 0:
  201.                     return ''
  202.                 
  203.                 break
  204.                 continue
  205.         
  206.     
  207.     return prefix
  208.  
  209.  
  210. def getsize(filename):
  211.     '''Return the size of a file, reported by os.stat()'''
  212.     return os.stat(filename).st_size
  213.  
  214.  
  215. def getmtime(filename):
  216.     '''Return the last modification time of a file, reported by os.stat()'''
  217.     return os.stat(filename).st_mtime
  218.  
  219.  
  220. def getatime(filename):
  221.     '''Return the last access time of a file, reported by os.stat()'''
  222.     return os.stat(filename).st_atime
  223.  
  224.  
  225. def getctime(filename):
  226.     '''Return the creation time of a file, reported by os.stat().'''
  227.     return os.stat(filename).st_ctime
  228.  
  229.  
  230. def islink(path):
  231.     '''Test for symbolic link.  On WindowsNT/95 always returns false'''
  232.     return False
  233.  
  234.  
  235. def exists(path):
  236.     '''Test whether a path exists'''
  237.     
  238.     try:
  239.         st = os.stat(path)
  240.     except os.error:
  241.         return False
  242.  
  243.     return True
  244.  
  245.  
  246. def isdir(path):
  247.     '''Test whether a path is a directory'''
  248.     
  249.     try:
  250.         st = os.stat(path)
  251.     except os.error:
  252.         return False
  253.  
  254.     return stat.S_ISDIR(st.st_mode)
  255.  
  256.  
  257. def isfile(path):
  258.     '''Test whether a path is a regular file'''
  259.     
  260.     try:
  261.         st = os.stat(path)
  262.     except os.error:
  263.         return False
  264.  
  265.     return stat.S_ISREG(st.st_mode)
  266.  
  267.  
  268. def ismount(path):
  269.     '''Test whether a path is a mount point (defined as root of drive)'''
  270.     (unc, rest) = splitunc(path)
  271.     if unc:
  272.         return rest in ('', '/', '\\')
  273.     
  274.     p = splitdrive(path)[1]
  275.     if len(p) == 1:
  276.         pass
  277.     return p[0] in '/\\'
  278.  
  279.  
  280. def walk(top, func, arg):
  281.     """Directory tree walk with callback function.
  282.  
  283.     For each directory in the directory tree rooted at top (including top
  284.     itself, but excluding '.' and '..'), call func(arg, dirname, fnames).
  285.     dirname is the name of the directory, and fnames a list of the names of
  286.     the files and subdirectories in dirname (excluding '.' and '..').  func
  287.     may modify the fnames list in-place (e.g. via del or slice assignment),
  288.     and walk will only recurse into the subdirectories whose names remain in
  289.     fnames; this can be used to implement a filter, or to impose a specific
  290.     order of visiting.  No semantics are defined for, or required of, arg,
  291.     beyond that arg is always passed to func.  It can be used, e.g., to pass
  292.     a filename pattern, or a mutable object designed to accumulate
  293.     statistics.  Passing None for arg is common."""
  294.     
  295.     try:
  296.         names = os.listdir(top)
  297.     except os.error:
  298.         return None
  299.  
  300.     func(arg, top, names)
  301.     exceptions = ('.', '..')
  302.     for name in names:
  303.         if name not in exceptions:
  304.             name = join(top, name)
  305.             if isdir(name):
  306.                 walk(name, func, arg)
  307.             
  308.         isdir(name)
  309.     
  310.  
  311.  
  312. def expanduser(path):
  313.     '''Expand ~ and ~user constructs.
  314.  
  315.     If user or $HOME is unknown, do nothing.'''
  316.     if path[:1] != '~':
  317.         return path
  318.     
  319.     (i, n) = (1, len(path))
  320.     while i < n and path[i] not in '/\\':
  321.         i = i + 1
  322.     if i == 1:
  323.         if 'HOME' in os.environ:
  324.             userhome = os.environ['HOME']
  325.         elif not ('HOMEPATH' in os.environ):
  326.             return path
  327.         else:
  328.             
  329.             try:
  330.                 drive = os.environ['HOMEDRIVE']
  331.             except KeyError:
  332.                 drive = ''
  333.  
  334.             userhome = join(drive, os.environ['HOMEPATH'])
  335.     else:
  336.         return path
  337.     return userhome + path[i:]
  338.  
  339.  
  340. def expandvars(path):
  341.     '''Expand shell variables of form $var and ${var}.
  342.  
  343.     Unknown variables are left unchanged.'''
  344.     if '$' not in path:
  345.         return path
  346.     
  347.     import string
  348.     varchars = string.ascii_letters + string.digits + '_-'
  349.     res = ''
  350.     index = 0
  351.     pathlen = len(path)
  352.     while index < pathlen:
  353.         c = path[index]
  354.         if c == "'":
  355.             path = path[index + 1:]
  356.             pathlen = len(path)
  357.             
  358.             try:
  359.                 index = path.index("'")
  360.                 res = res + "'" + path[:index + 1]
  361.             except ValueError:
  362.                 res = res + path
  363.                 index = pathlen - 1
  364.             except:
  365.                 None<EXCEPTION MATCH>ValueError
  366.             
  367.  
  368.         None<EXCEPTION MATCH>ValueError
  369.         if c == '$':
  370.             if path[index + 1:index + 2] == '$':
  371.                 res = res + c
  372.                 index = index + 1
  373.             elif path[index + 1:index + 2] == '{':
  374.                 path = path[index + 2:]
  375.                 pathlen = len(path)
  376.                 
  377.                 try:
  378.                     index = path.index('}')
  379.                     var = path[:index]
  380.                     if var in os.environ:
  381.                         res = res + os.environ[var]
  382.                 except ValueError:
  383.                     res = res + path
  384.                     index = pathlen - 1
  385.                 except:
  386.                     None<EXCEPTION MATCH>ValueError
  387.                 
  388.  
  389.             None<EXCEPTION MATCH>ValueError
  390.             var = ''
  391.             index = index + 1
  392.             c = path[index:index + 1]
  393.             while c != '' and c in varchars:
  394.                 var = var + c
  395.                 index = index + 1
  396.                 c = path[index:index + 1]
  397.             if var in os.environ:
  398.                 res = res + os.environ[var]
  399.             
  400.             if c != '':
  401.                 res = res + c
  402.             
  403.         else:
  404.             res = res + c
  405.         index = index + 1
  406.     return res
  407.  
  408.  
  409. def normpath(path):
  410.     '''Normalize path, eliminating double slashes, etc.'''
  411.     path = path.replace('/', '\\')
  412.     (prefix, path) = splitdrive(path)
  413.     while path[:1] == '\\':
  414.         prefix = prefix + '\\'
  415.         path = path[1:]
  416.     comps = path.split('\\')
  417.     i = 0
  418.     while i < len(comps):
  419.         if comps[i] in ('.', ''):
  420.             del comps[i]
  421.             continue
  422.         if comps[i] == '..':
  423.             if i > 0 and comps[i - 1] != '..':
  424.                 del comps[i - 1:i + 1]
  425.                 i -= 1
  426.             elif i == 0 and prefix.endswith('\\'):
  427.                 del comps[i]
  428.             else:
  429.                 i += 1
  430.         prefix.endswith('\\')
  431.         i += 1
  432.     if not prefix and not comps:
  433.         comps.append('.')
  434.     
  435.     return prefix + '\\'.join(comps)
  436.  
  437.  
  438. def abspath(path):
  439.     '''Return the absolute version of a path'''
  440.     global abspath
  441.     
  442.     try:
  443.         _getfullpathname = _getfullpathname
  444.         import nt
  445.     except ImportError:
  446.         
  447.         def _abspath(path):
  448.             if not isabs(path):
  449.                 path = join(os.getcwd(), path)
  450.             
  451.             return normpath(path)
  452.  
  453.         abspath = _abspath
  454.         return _abspath(path)
  455.  
  456.     if path:
  457.         
  458.         try:
  459.             path = _getfullpathname(path)
  460.         except WindowsError:
  461.             pass
  462.         except:
  463.             None<EXCEPTION MATCH>WindowsError
  464.         
  465.  
  466.     None<EXCEPTION MATCH>WindowsError
  467.     path = os.getcwd()
  468.     return normpath(path)
  469.  
  470. realpath = abspath
  471. if hasattr(sys, 'getwindowsversion'):
  472.     pass
  473. supports_unicode_filenames = sys.getwindowsversion()[3] >= 2
  474.